﻿using System;
using System.Drawing;
using System.IO;
using System.Reflection;
using WinForm.Properties;

namespace WinForm.Common
{
    /// <summary>
    /// Small16  16*16; Large32  32*32;
    /// </summary>
    public enum ImageSize { Small16, Large32, None }

    /// <summary>
    /// 为读取资源文件中的字符串、图片等提供统一方便的方法
    /// 需要把资源文件的“自定义工具”值设置为“PublicResXFileCodeGenerator”，以便资源文件中的“属性”变为public
    /// </summary>
    
    public class ResourcesHelper
    {
        private static string projectName = "WinForm";

        #region GetString
        /// <summary>
        /// 返回资源文件中字符串值,默认资源文件在Properties文件夹下，文件名为Resources
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetString(string name)
        {
            PropertyInfo pi = typeof(Resources).GetProperty(name);
            if (pi == null) return string.Format("Havn't Find the string \"{0}\"", name);
            return string.Format("{0}", pi.GetValue(null, new object[] { }));
        }

        ///// <summary>
        ///// 返回程序集中资源文件的字符串值
        ///// </summary>
        ///// <param name="name">字符串</param>
        ///// <param name="resourceFilename">资源文件名</param>
        ///// <param name="asmName">程序集名称字符串</param>
        ///// <returns></returns>
        //public static string GetString(string name, string resourceFilename, string asmName)
        //{
        //    Assembly asm = Assembly.Load(asmName);//Load方法如果已经加载一个相同标识的程序集只会简单的返回这个程序集的引用，而不会去创建一个新的程序集。
        //    return GetStringBasic(name, resourceFilename, asm);
        //}


        /// <summary>
        /// 返回程序集中资源文件的字符串值
        /// </summary>
        /// <param name="name">字符串</param>
        /// <param name="resourceFilename">资源文件名</param>
        /// <param name="asm">程序集</param>
        /// <returns></returns>
        public static string GetString(string name, string resourceFilename, Assembly asm)
        {
            return GetStringBasic(name, resourceFilename, asm);
        }
        /// <summary>
        /// 返回程序集中资源文件的字符串值
        /// </summary>
        /// <param name="name">字符串</param>
        /// <param name="resourceFilename">资源文件名</param>
        /// <param name="asm">程序集</param>
        /// <returns></returns>
        private static string GetStringBasic(string name, string resourceFilename, Assembly asm)
        {
            Type t = asm.GetType(string.Format("{0}.{1}",asm.GetName().Name,resourceFilename));
            PropertyInfo pi = t.GetProperty(name);
            return string.Format("{0}", pi.GetValue(null, new object[] { }));
        }

        #endregion GetString

        ///// <summary>
        ///// 从嵌入的资源图片中获取图片，由程序员保证程序中name字符串的图片存在，否则异常
        /////     * 例如使用“About.png”文件，
        /////     * 首先， 把文件添加到项目中，
        /////     * 然后， 再“About.png”的属性“生成操作”设置为“嵌入的资源”
        /////     * 最后， 把CreateImageFromResources(string name, assemble asm)的参数name值设置为 "项目名称"+"."+"文件夹名称"+"."+"文件全名"
        ///// </summary>
        ///// <param name="name">图片文件名字："项目名称"+"."+"文件夹名称"+"."+"文件全名"</param>
        ///// <param name="asm"></param>
        ///// <returns></returns>
        private static Image GetImageBasic(string name, Assembly asm)
        {
            Stream manifestResourceStream = asm.GetManifestResourceStream(name);
            if (manifestResourceStream == null) return null;
            return Image.FromStream(manifestResourceStream);
        }
        ///// <summary>
        ///// 从资源文件中获取图片，需要预先把图片添加到资源中
        /////  </summary>
        ///// <param name="name">图片文件名字："项目名称"+"."+"文件夹名称"+"."+"文件全名"</param>
        ///// <returns></returns>
        //public static Image GetImageFromResourcesFile(string name)
        //{
        //    Assembly asm = typeof (ResourcesHelper).Assembly;
        //    return GetImageBasic(name, asm);
        //}

        /// <summary>
        /// 根据文件名获取文件夹Images中的图片
        /// </summary>
        /// <param name="name">图片文件名，该图片应该放在Images文件夹中,且属性“生成操作”设置为“嵌入的资源”</param>
        /// <param name="size">图片大小</param>
        /// <returns></returns>
        public static Image GetImageFromImagesFolder(string name, ImageSize size)
        {
            if (string.IsNullOrEmpty(name)) return null;
            string fullFileName = ImageSize.None == size  ? string.Format("{0}.Images.{1}.png", projectName, name) : string.Format("{0}.Images.{1}_{2}.png", projectName, name, GetImageSizeString(size));
            Assembly asm = typeof(ResourcesHelper).Assembly;
            return GetImageBasic(fullFileName,asm);
        }

        private static string GetImageSizeString(ImageSize size)
        {
            if (size == ImageSize.Small16) return "16x16";
            return "32x32";
        }

        public static Icon GetIconFromImagesFolder(string name)
        {
            Assembly asm = typeof(ResourcesHelper).Assembly;
            Stream manifestResourceStream = asm.GetManifestResourceStream(string.Format("{0}.Images.{1}", projectName, name));
            return manifestResourceStream == null ? null : new Icon(manifestResourceStream);
        }
    }
}
